package com.miracle9.game.service;

import com.miracle9.game.dao.AdminServiceDao;
import com.miracle9.game.dao.NewRegistServiceDao;
import com.miracle9.game.dao.AdminLogServiceDao;
import com.miracle9.game.dao.OnlineCountServiceDao;
import com.miracle9.game.dao.PayLogServiceDao;
import com.miracle9.game.dao.BusineAccountsServiceDao;
import com.miracle9.game.dao.PromoterServiceDao1;
import com.miracle9.game.dao.SystemConfigServiceDao;
import com.miracle9.game.dao.UserAwardServiceDao;
import com.miracle9.game.dao.UserServiceDao;
import com.miracle9.game.dao.ChargeStatisServiceDao;
import com.miracle9.game.dao.ComputerOrderServiceDao;
import com.miracle9.game.dao.DailyActiveServiceDao;
import com.miracle9.game.dao.DayAccountsServiceDao;
import com.miracle9.game.dao.GameDataLogServiceDao;
import com.miracle9.game.bean.Pager;
import com.miracle9.game.entity.Admin;
import com.miracle9.game.entity.AdminLog;
import com.miracle9.game.entity.BusineAccounts;
import com.miracle9.game.entity.ChargeStatis;
import com.miracle9.game.entity.ComputerOrder;
import com.miracle9.game.entity.DailyActive;
import com.miracle9.game.entity.DayAccounts;
import com.miracle9.game.entity.NewRegist;
import com.miracle9.game.entity.OnlineCount;
import com.miracle9.game.entity.PayLog;
import com.miracle9.game.entity.Promoter;
import com.miracle9.game.entity.SystemConfig;
import com.miracle9.game.entity.User;
import com.miracle9.game.entity.UserAward;
import com.miracle9.game.util.MyUtil;

import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("statementService")
public class StatementService extends BaseService {

    @Autowired
    private PayLogServiceDao payLogServiceDao;
    @Autowired
    private UserAwardServiceDao userAwardServiceDao;
    @Autowired
    private SystemConfigServiceDao systemConfigServiceDao;
    @Autowired
    private UserServiceDao userServiceDao;
    @Autowired
    private AdminLogServiceDao adminLogServiceDao;
    @Autowired
    private NewRegistServiceDao newRegistServiceDao;
    @Autowired
    private DailyActiveServiceDao dailyActiveServiceDao;
    @Autowired
    private OnlineCountServiceDao onlineCountServiceDao;
    @Autowired
    private ChargeStatisServiceDao chargeStatisServiceDao;
    @Autowired
    private ComputerOrderServiceDao computerOrderServiceDao;
    @Autowired
    private AdminServiceDao adminServiceDao;
    @Autowired
    private BusineAccountsServiceDao busineAccountsServiceDao;
    @Autowired
    private GameDataLogServiceDao gameDataLogServiceDao;
    @Autowired
    private DayAccountsServiceDao dayAccountsServiceDao;
    @Autowired
    private DayAccountsService dayAccountsService;
    @Autowired
    private PromoterServiceDao1 promoterServiceDao1;

    public Object[] getPayReport(String date, int userType, IoSession session) {
        String hql = null;
        if (userType == 0) {
            hql = "from PayLog where type=0 and userType=0 and parentPromoterId =0 and datetime like ? order by datetime desc";
        } else if (userType == 1) {
            hql = "from PayLog where type=0 and userType=1 and parentPromoterId =0 and datetime like ? order by datetime desc";
        } else {
            hql = "from PayLog where type=0 and parentPromoterId =0 and datetime like ? order by datetime desc";
        }
        List datas = new ArrayList();
        List<PayLog> pays = this.payLogServiceDao.q_CreateSqlList(hql, new Object[]{date + "%"});
        for (PayLog p : pays) {
            Map data = new HashMap();
            data.put("date", p.getDatetime());
            String authority = AdminService.getAuthStr(Integer.valueOf(resultInt(session)), p.getAuthority());
            data.put("authority", authority);
            data.put("operator", p.getAdmin());
            data.put("user", p.getUsername());
            data.put("gameGold", Integer.valueOf(p.getGold()));
            data.put("charge", Double.valueOf(p.getMoney()));
            datas.add(data);
        }
        return datas.toArray();
    }

    public Object[] getExchangeReport(String date, int userType, IoSession session) {
        String hql = null;
        if (userType == 0) {
            hql = "from PayLog where type=1 and userType=0 and parentPromoterId =0 and datetime like ? order by datetime desc";
        } else if (userType == 1) {
            hql = "from PayLog where type=1 and userType=1 and parentPromoterId =0 and datetime like ? order by datetime desc";
        } else {
            hql = "from PayLog where type=1 and parentPromoterId =0 and datetime like ?";
        }
        List datas = new ArrayList();
        List<PayLog> pays = this.payLogServiceDao.q_CreateSqlList(hql, new Object[]{date + "%"});
        for (PayLog p : pays) {
            Map data = new HashMap();
            data.put("date", p.getDatetime());
            String authority = AdminService.getAuthStr(Integer.valueOf(resultInt(session)), p.getAuthority());
            data.put("authority", authority);
            data.put("operator", p.getAdmin());
            data.put("user", p.getUsername());
            data.put("gameGold", Integer.valueOf(p.getGold()));
            data.put("changeValue", Double.valueOf(p.getMoney()));
            datas.add(data);
        }
        return datas.toArray();
    }

    public Object[] getDayAccount(String date, int userType, IoSession session) {
        String hql = null;
        if (userType == 0) {
            hql = "from PayLog where userType=0 and parentPromoterId =0 and datetime like ? order by datetime desc";
        } else if (userType == 1) {
            hql = "from PayLog where userType=1 and parentPromoterId =0 and datetime like ? order by datetime desc";
        } else {
            hql = "from PayLog where parentPromoterId =0 and datetime like ? order by datetime desc";
        }
        List datas = new ArrayList();
        List<PayLog> pays = this.payLogServiceDao.q_CreateSqlList(hql, new Object[]{date + "%"});
        String authority;
        for (PayLog p : pays) {
            Map data = new HashMap();
            data.put("date", p.getDatetime());
            authority = AdminService.getAuthStr(Integer.valueOf(resultInt(session)), p.getAuthority());
            data.put("authority", authority);
            data.put("operator", p.getAdmin());
            if (p.getType() == 0) {
                data.put("type", MyUtil.getProperties(Integer.valueOf(resultInt(session)), "pay"));
            } else if (p.getType() == 1) {
                data.put("type", MyUtil.getProperties(Integer.valueOf(resultInt(session)), "expiry"));
            } else if (p.getType() == 2) {
                data.put("type", MyUtil.getProperties(Integer.valueOf(resultInt(session)), "kouChu"));
            }
            data.put("user", p.getUsername());
            data.put("gameGold", Integer.valueOf(p.getGold()));
            data.put("changeValue", Double.valueOf(p.getMoney()));
            datas.add(data);
        }
        if (userType != 1) {
            List<UserAward> uas = this.userAwardServiceDao.q_CreateSqlList("from UserAward where datetime like ? order by id desc", new Object[]{date + "%"});
            Map data;
            for (UserAward u : uas) {
                SystemConfig config = this.systemConfigServiceDao.getSystemConfig();
                data = new HashMap();
                data.put("date", u.getDatetime());
                authority = AdminService.getAuthStr(Integer.valueOf(resultInt(session)), u.getAuthority());
                data.put("authority", authority);
                data.put("operator", u.getAdmin());
                data.put("type", MyUtil.getProperties(Integer.valueOf(resultInt(session)), "zengSong"));
                data.put("user", u.getUsername());
                data.put("gameGold", Integer.valueOf(u.getGold()));
                double dou = MyUtil.doubleDivideCalculation(u.getGold() * config.getPayScale(), 100.0D);
                data.put("changeValue", Double.valueOf(MyUtil.returnBigDecimalDoubleValue(dou)));
                datas.add(data);
            }
            List<ComputerOrder> orders = this.computerOrderServiceDao.q_CreateSqlList("from ComputerOrder where borrowDate like ? and user.promoterId=0 order by borrowDate desc", new Object[]{date + "%"});
            for (ComputerOrder o : orders) {
                data = new HashMap();
                data.put("date", o.getBorrowDate());
                authority = AdminService.getAuthStr(Integer.valueOf(resultInt(session)), o.getBorrowAuthority());
                data.put("authority", authority);
                data.put("operator", o.getAdmin());
                data.put("type", MyUtil.getProperties(Integer.valueOf(resultInt(session)), "userZhujie"));
                data.put("user", o.getUser().getUsername());
                data.put("gameGold", Integer.valueOf(0));
                data.put("changeValue", Integer.valueOf(o.getDeposit()));
                datas.add(data);
            }
            orders = this.computerOrderServiceDao.q_CreateSqlList("from ComputerOrder where repayDate like ? and user.promoterId=0 order by repayDate desc", new Object[]{date + "%"});
            for (ComputerOrder o : orders) {
                data = new HashMap();
                data.put("date", o.getRepayDate());
                authority = AdminService.getAuthStr(Integer.valueOf(resultInt(session)), o.getRepayAuthority());
                data.put("authority", authority);
                data.put("operator", o.getRepayAdmin());
                data.put("type", MyUtil.getProperties(Integer.valueOf(resultInt(session)), "userGuihai"));
                data.put("user", o.getUser().getUsername());
                data.put("gameGold", Integer.valueOf(0));
                data.put("changeValue", Integer.valueOf(o.getReturnDeposit()));
                datas.add(data);
            }
        }
        return datas.toArray();
    }

    public Pager getUserAdminLog(String startDate, String endDate, Pager pager, IoSession session) {
        endDate = MyUtil.DateByDayCalendar(endDate, 1);
        String orgOrderBy = pager.getOrderBy();
        if (pager.getOrderBy() == "") {
            pager.setOrderBy("datetime");
            pager.setOrder("desc");
        } else if ((orgOrderBy != null) && (orgOrderBy.equals("content")) && (resultInt(session) == 1)) {
            pager.setOrderBy("contentEn");
        }
        pager = this.adminLogServiceDao.q_CreateSqlsPager("from AdminLog where type=0 and datetime>=? and datetime<?", pager, new Object[]{startDate, endDate});
        List datas = new ArrayList();
        for (Object o : pager.getList()) {
            AdminLog adminLog = (AdminLog) o;
            Map data = new HashMap();
            data.put("datetime", adminLog.getDatetime());
            String authority = AdminService.getAuthStr(Integer.valueOf(resultInt(session)), adminLog.getAuthority());
            data.put("authority", authority);
            if (resultInt(session) == 0) {
                data.put("content", adminLog.getContent());
            } else {
                data.put("content", adminLog.getContentEn());
            }
            data.put("admin", adminLog.getAdmin());
            datas.add(data);
        }
        pager.setList(datas.toArray());
        pager.setOrderBy(orgOrderBy);
        return pager;
    }

    public Pager getGameAdminLog(String startDate, String endDate, Pager pager, IoSession session) {
        endDate = MyUtil.DateByDayCalendar(endDate, 1);
        String orgOrderBy = pager.getOrderBy();
        if (pager.getOrderBy() == "") {
            pager.setOrderBy("datetime");
            pager.setOrder("desc");
        } else if ((orgOrderBy != null) && (orgOrderBy.equals("content")) && (resultInt(session) == 1)) {
            pager.setOrderBy("contentEn");
        }
        pager = this.adminLogServiceDao.q_CreateSqlsPager("from AdminLog where type=1 and datetime>=? and datetime<?", pager, new Object[]{startDate, endDate});
        List datas = new ArrayList();
        for (Object o : pager.getList()) {
            AdminLog adminLog = (AdminLog) o;
            Map data = new HashMap();
            data.put("datetime", adminLog.getDatetime());
            String authority = AdminService.getAuthStr(Integer.valueOf(resultInt(session)), adminLog.getAuthority());
            data.put("authority", authority);
            if (resultInt(session) == 0) {
                data.put("content", adminLog.getContent());
            } else {
                data.put("content", adminLog.getContentEn());
            }
            data.put("admin", adminLog.getAdmin());
            datas.add(data);
        }
        pager.setList(datas.toArray());
        pager.setOrderBy(orgOrderBy);
        return pager;
    }

    public Object[] getMonthAccount(String date, int type, IoSession session) {
        System.out.println("date " + date + "  type " + type);
        List datas = new ArrayList();
        try {
            if (type == 0) {
                List<DayAccounts> accounts = this.dayAccountsServiceDao.q_CreateSqlList("from DayAccounts where datetime like ? order by datetime desc", new Object[]{date + "%"});

                System.out.println(MyUtil.dateFormatYMD_Sting(new Date()));

                DayAccounts dayAccounts = null;
                try {
                    dayAccounts = this.dayAccountsService.generateAccounts(MyUtil.dateFormatYMD_Sting(new Date()));
                } catch (Exception localException1) {
                }
                accounts.add(accounts.size(), dayAccounts);
                List dates = datesList(date);
                /*for (String d : dates)
                {
                  DayAccounts day = null;
                  for (DayAccounts b : accounts) {
                    if (d.equals(b.getDatetime()))
                    {
                      day = b;
                      break;
                    }
                  }
                  if (day == null)
                  {
                    Map data = new HashMap();
                    data.put("date", d);
                    data.put("charge", Integer.valueOf(0));
                    data.put("change", Integer.valueOf(0));
                    data.put("deposit", Integer.valueOf(0));
                    data.put("returnDeposit", Integer.valueOf(0));
                    data.put("minus", Integer.valueOf(0));
                    data.put("give", Integer.valueOf(0));
                    data.put("allCoin", Integer.valueOf(0));
                    data.put("result", Integer.valueOf(0));
                    datas.add(data);
                  }
                  else
                  {
                    Map data = new HashMap();
                    data.put("date", day.getDatetime());
                    data.put("charge", Integer.valueOf(day.getPayCount()));
                    data.put("change", Double.valueOf(day.getExchangeCount()));
                    data.put("deposit", Integer.valueOf(day.getTakeDeposit()));
                    data.put("returnDeposit", Integer.valueOf(day.getReturnDeposit()));
                    data.put("minus", Integer.valueOf(day.getDeductGold()));
                    data.put("give", Integer.valueOf(day.getGiveGold()));
                    if (day.getAllSumGold() == 0L) {
                      data.put("allCoin", Long.valueOf(day.getSumGold()));
                    } else {
                      data.put("allCoin", Long.valueOf(day.getAllSumGold()));
                    }
                    data.put("result", Double.valueOf(day.getRevenue()));
                    datas.add(data);
                  }
                }*/
                for (int i = 0; i < dates.size(); i++) {
                    DayAccounts day = null;
                    for (DayAccounts b : accounts) {
                        if (dates.get(i).equals(b.getDatetime())) {
                            day = b;
                            break;
                        }
                    }
                    if (day == null) {
                        Map data = new HashMap();
                        data.put("date", dates.get(i));
                        data.put("charge", Integer.valueOf(0));
                        data.put("change", Integer.valueOf(0));
                        data.put("deposit", Integer.valueOf(0));
                        data.put("returnDeposit", Integer.valueOf(0));
                        data.put("minus", Integer.valueOf(0));
                        data.put("give", Integer.valueOf(0));
                        data.put("allCoin", Integer.valueOf(0));
                        data.put("result", Integer.valueOf(0));
                        datas.add(data);
                    } else {
                        Map data = new HashMap();
                        data.put("date", day.getDatetime());
                        data.put("charge", Integer.valueOf(day.getPayCount()));
                        data.put("change", Double.valueOf(day.getExchangeCount()));
                        data.put("deposit", Integer.valueOf(day.getTakeDeposit()));
                        data.put("returnDeposit", Integer.valueOf(day.getReturnDeposit()));
                        data.put("minus", Integer.valueOf(day.getDeductGold()));
                        data.put("give", Integer.valueOf(day.getGiveGold()));
                        if (day.getAllSumGold() == 0L) {
                            data.put("allCoin", Long.valueOf(day.getSumGold()));
                        } else {
                            data.put("allCoin", Long.valueOf(day.getAllSumGold()));
                        }
                        data.put("result", Double.valueOf(day.getRevenue()));
                        datas.add(data);
                    }
                }
            } else {
                List<BusineAccounts> bs = this.busineAccountsServiceDao.q_CreateSqlList("from BusineAccounts where startDate like ? order by startDate asc", new Object[]{date + "%"});
                int i = 1;
                for (BusineAccounts b : bs) {
                    Object data = new HashMap();
                    ((Map) data).put("date", Integer.valueOf(i));
                    ((Map) data).put("charge", Integer.valueOf(b.getPayCount()));
                    ((Map) data).put("change", Double.valueOf(b.getExchangeCount()));
                    ((Map) data).put("deposit", Integer.valueOf(b.getTakeDeposit()));
                    ((Map) data).put("returnDeposit", Integer.valueOf(b.getReturnDeposit()));
                    ((Map) data).put("minus", Integer.valueOf(b.getDeductGold()));
                    ((Map) data).put("give", Integer.valueOf(b.getGiveGold()));
                    ((Map) data).put("result", Double.valueOf(b.getRevenue()));
                    datas.add(data);
                    i++;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return datas.toArray();
    }

    public Map<String, Object> deleteMonthAccount(String date, IoSession session) {
        Map<String, Object> result = new HashMap<>();
        try {
            this.payLogServiceDao.deleteByDate(date);
            this.userAwardServiceDao.deleteByDate(date);
            this.computerOrderServiceDao.updates(date);
            this.gameDataLogServiceDao.deleteByDate(date);
            this.dayAccountsServiceDao.deleteByDate(date);
            this.busineAccountsServiceDao.deleteByDate(date);
            this.userServiceDao.deleteByDateBlurry(date);
            result.put("success", Boolean.TRUE);
            result.put("message", "");
        } catch (Exception e) {
            result.put("success", Boolean.FALSE);
            result.put("message", MyUtil.getSessionAttribute(session, "systemError"));
        }
        return result;
    }

    public Pager getSystemLog(int userId, String startDate, String endDate, Pager pager, IoSession session) {
        endDate = MyUtil.DateByDayCalendar(endDate, 1);
        String orgOrderBy = pager.getOrderBy();
        if (pager.getOrderBy() == "") {
            pager.setOrderBy("datetime");
            pager.setOrder("desc");
        } else if ((orgOrderBy != null) && (orgOrderBy.equals("content")) && (resultInt(session) == 1)) {
            pager.setOrderBy("contentEn");
        }
        if (userId == -1) {
            pager = this.adminLogServiceDao.q_CreateSqlsPager("from AdminLog where datetime>=? and datetime<?", pager, new Object[]{startDate, endDate});
        } else {
            Admin a = (Admin) this.adminServiceDao.getById(Integer.valueOf(userId));
            if (a != null) {
                pager = this.adminLogServiceDao.q_CreateSqlsPager("from AdminLog where admin=? and datetime>=? and datetime<?", pager, new Object[]{a.getUsername(), startDate, endDate});
            } else {
                pager.setList(new AdminLog[0]);
            }
        }
        List datas = new ArrayList();
        for (Object o : pager.getList()) {
            AdminLog adminLog = (AdminLog) o;
            Map data = new HashMap();
            data.put("datetime", adminLog.getDatetime());
            String authority = AdminService.getAuthStr(Integer.valueOf(resultInt(session)), adminLog.getAuthority());
            data.put("authority", authority);
            if (resultInt(session) == 0) {
                data.put("content", adminLog.getContent());
            } else {
                data.put("content", adminLog.getContentEn());
            }
            data.put("admin", adminLog.getAdmin());
            datas.add(data);
        }
        pager.setList(datas.toArray());
        pager.setOrderBy(orgOrderBy);
        return pager;
    }

    public Object[] getNewRegist(String startDate, String endDate, int userType, IoSession session) {
        List datas = new ArrayList();
        Calendar startTime = Calendar.getInstance();
        Calendar endTime = Calendar.getInstance();
        startTime.setTime(MyUtil.dateFormatYMD(startDate));
        endTime.setTime(MyUtil.dateFormatYMD(endDate));
        Calendar now = Calendar.getInstance();
        now.set(11, 0);
        now.set(12, 0);
        now.set(13, 0);
        now.set(14, 0);
        if ((startTime.getTimeInMillis() <= endTime.getTimeInMillis()) && (endTime.getTimeInMillis() >= now.getTimeInMillis()) && (startTime.getTimeInMillis() <= now.getTimeInMillis())) {
            String currentDate = MyUtil.dateFormatYMD_Sting(new Date());
            int registCount = 0;
            if (userType == 0) {
                registCount = this.userServiceDao.q_CreateSqlInt("select count(*) from User where promoterId=0 and registDate>=? order by registDate desc ", new Object[]{currentDate});
            } else if (userType == 1) {
                registCount = this.userServiceDao.q_CreateSqlInt("select count(*) from User where promoterId!=0 and registDate>=? order by registDate desc ", new Object[]{currentDate});
            } else {
                registCount = this.userServiceDao.q_CreateSqlInt("select count(*) from User where registDate>=? order by registDate desc", new Object[]{currentDate});
            }
            Map data = new HashMap();
            data.put("date", currentDate);
            data.put("registCount", Integer.valueOf(registCount));
            datas.add(data);
        }
        endDate = MyUtil.DateByDayCalendar(endDate, 1);
        List<NewRegist> nrs = this.newRegistServiceDao.getNewRegistsBy(startDate, endDate, userType);
        for (NewRegist n : nrs) {
            Map data = new HashMap();
            data.put("date", n.getDatetime());
            data.put("registCount", Integer.valueOf(n.getCount()));
            datas.add(data);
        }
        return datas.toArray();
    }

    public Object[] getDailyActive(String startDate, String endDate, int userType, IoSession session) {
        List datas = new ArrayList();
        Calendar startTime = Calendar.getInstance();
        Calendar endTime = Calendar.getInstance();
        startTime.setTime(MyUtil.dateFormatYMD(startDate));
        endTime.setTime(MyUtil.dateFormatYMD(endDate));
        Calendar now = Calendar.getInstance();
        now.set(11, 0);
        now.set(12, 0);
        now.set(13, 0);
        now.set(14, 0);
        if ((startTime.getTimeInMillis() <= endTime.getTimeInMillis()) && (endTime.getTimeInMillis() >= now.getTimeInMillis()) && (startTime.getTimeInMillis() <= now.getTimeInMillis())) {
            String currentDate = MyUtil.dateFormatYMD_Sting(new Date());
            int activeCount = 0;
            if (userType == 0) {
                activeCount = this.userServiceDao.q_CreateSqlInt("select count(*) from User where promoterId=0 and loginDate>=? order by loginDate desc ", new Object[]{currentDate});
            } else if (userType == 1) {
                activeCount = this.userServiceDao.q_CreateSqlInt("select count(*) from User where promoterId!=0 and loginDate>=? order by loginDate desc", new Object[]{currentDate});
            } else {
                activeCount = this.userServiceDao.q_CreateSqlInt("select count(*) from User where loginDate>=? order by loginDate desc", new Object[]{currentDate});
            }
            Map data = new HashMap();
            data.put("date", currentDate);
            data.put("activeCount", Integer.valueOf(activeCount));
            datas.add(data);
        }
        endDate = MyUtil.DateByDayCalendar(endDate, 1);
        List<DailyActive> das = this.dailyActiveServiceDao.getDailyActivesByTypeAndInterval(startDate, endDate, userType);
        for (DailyActive d : das) {
            Map data = new HashMap();
            data.put("date", d.getDatetime());
            data.put("activeCount", Integer.valueOf(d.getCount()));
            datas.add(data);
        }
        return datas.toArray();
    }

    public Object[] getOnlineCount(String startDate, String endDate, int userType, IoSession session) {
        List datas = new ArrayList();

        endDate = MyUtil.DateByDayCalendar(endDate, 1);
        List<OnlineCount> ocs = new ArrayList<>();
        if(userType==2){
            ocs = this.onlineCountServiceDao.getOnlineCountsBy(startDate, endDate);
        } else {
            ocs = this.onlineCountServiceDao.getOnlineCountsBy(startDate, endDate, userType);
        }
        for (OnlineCount o : ocs) {
            Map data = new HashMap();
            data = new HashMap();
            data.put("date", o.getDatetime());
            data.put("onlineCount", Integer.valueOf(o.getOnlineCount()));
            data.put("info", o.getDetail());
            datas.add(data);
        }
        return datas.toArray();
    }

    public Object[] getStay(String startDate, String endDate, int userType, IoSession session) {
        List datas = new ArrayList();
        try {
            Calendar startTime = Calendar.getInstance();
            Calendar endTime = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            startTime.setTime(sdf.parse(startDate));
            endTime.setTime(sdf.parse(endDate));
            if (startTime.getTimeInMillis() <= endTime.getTimeInMillis()) {
                List<String> times = new ArrayList();
                times.add(endDate);
                while (endTime.after(startTime)) {
                    endTime.add(6, -1);
                    times.add(sdf.format(endTime.getTime()));
                }
                for (String date : times) {
                    List registUsers = this.userServiceDao.ifGetId(date, userType);
                    Map data = new HashMap();
                    data.put("date", date);
                    data.put("new", Integer.valueOf(registUsers.size()));
                    int[] indexArray = {1, 2, 3, 4, 5, 6, 7, 14, 30};
                    for (int index : indexArray) {
                        Calendar lcTime = Calendar.getInstance();
                        lcTime.setTime(sdf.parse(date));
                        lcTime.add(6, index);
                        if (lcTime.getTimeInMillis() < System.currentTimeMillis()) {
                            int count = 0;
                            if (!registUsers.isEmpty()) {
                                count = this.userServiceDao.getQuantityByDateAndIds(registUsers, MyUtil.dateFormatYMD_Sting(lcTime.getTime()));
                            }
                            switch (index) {
                                case 1:
                                    data.put("day1", Integer.valueOf(count));
                                    break;
                                case 2:
                                    data.put("day2", Integer.valueOf(count));
                                    break;
                                case 3:
                                    data.put("day3", Integer.valueOf(count));
                                    break;
                                case 4:
                                    data.put("day4", Integer.valueOf(count));
                                    break;
                                case 5:
                                    data.put("day5", Integer.valueOf(count));
                                    break;
                                case 6:
                                    data.put("day6", Integer.valueOf(count));
                                    break;
                                case 7:
                                    data.put("day7", Integer.valueOf(count));
                                    break;
                                case 14:
                                    data.put("day14", Integer.valueOf(count));
                                    break;
                                case 30:
                                    data.put("day30", Integer.valueOf(count));
                            }
                        } else {
                            data.put("day" + index, "---");
                        }
                    }
                    datas.add(data);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return datas.toArray();
    }

    public Object[] getCharge(String startDate, String endDate, int userType, IoSession session) {
        List datas = new ArrayList();
        Calendar startTime = Calendar.getInstance();
        Calendar endTime = Calendar.getInstance();
        startTime.setTime(MyUtil.dateFormatYMD(startDate));
        endTime.setTime(MyUtil.dateFormatYMD(endDate));
        Calendar now = Calendar.getInstance();
        now.set(11, 0);
        now.set(12, 0);
        now.set(13, 0);
        now.set(14, 0);
        Set userIdSet;
        if ((startTime.getTimeInMillis() <= endTime.getTimeInMillis()) && (endTime.getTimeInMillis() >= now.getTimeInMillis()) && (startTime.getTimeInMillis() <= now.getTimeInMillis())) {
            String currentDate = MyUtil.dateFormatYMD_Sting(new Date());
            List<PayLog> payLogs = null;
            if (userType == 0) {
                payLogs = this.payLogServiceDao.q_CreateSqlList("from PayLog where parentPromoterId=0 and type=0 and datetime>=?", new Object[]{currentDate});
            } else if (userType == 1) {
                payLogs = this.payLogServiceDao.q_CreateSqlList("from PayLog where parentPromoterId!=0 and type=0 and datetime>=?", new Object[]{currentDate});
            } else {
                payLogs = this.payLogServiceDao.q_CreateSqlList("from PayLog where type=0 and datetime>=?", new Object[]{currentDate});
            }
            int count = payLogs.size();
            userIdSet = new HashSet();
            int amount = 0;
            for (PayLog p : payLogs) {
                userIdSet.add(Integer.valueOf(p.getUserId()));
                amount = (int) (amount + p.getMoney());
            }
            int user_count = userIdSet.size();
            int money = 0;
            if (count > 0) {
                money = amount / count;
            }
            int arpu = 0;
            if (user_count > 0) {
                arpu = amount / user_count;
            }
            Map data = new HashMap();
            data.put("date", currentDate);
            data.put("userCount", Integer.valueOf(user_count));
            data.put("payCount", Integer.valueOf(count));
            data.put("all", Integer.valueOf(amount));
            data.put("avg", Integer.valueOf(money));
            data.put("arpu", Integer.valueOf(arpu));
            datas.add(data);
        }
        endDate = MyUtil.DateByDayCalendar(endDate, 1);
        List<ChargeStatis> css = this.chargeStatisServiceDao.getByTypeAndInterval(startDate, endDate, userType);
        for (ChargeStatis c : css) {
            Map data = new HashMap();
            data.put("date", c.getDatetime());
            data.put("userCount", Integer.valueOf(c.getUserCount()));
            data.put("payCount", Integer.valueOf(c.getCount()));
            data.put("all", Integer.valueOf(c.getAmount()));
            data.put("avg", Integer.valueOf(c.getMoney()));
            data.put("arpu", Integer.valueOf(c.getArpu()));
            datas.add(data);
        }
        return datas.toArray();
    }

    public long getServerTime(IoSession session) {
        return System.currentTimeMillis();
    }

    public Object[] getRunStatistics(String date, int type, int promoterId, IoSession session) {
        System.out.println("StatementService.getRunStatistics  date " + date + " type " + type + " promoterId " + promoterId);
        List result = new ArrayList();
        List<String> dates = datesList(date);
        StringBuilder proIds = new StringBuilder();
        String hql = null;
        if (promoterId == -1) {
            if (type == 7) {
                hql = "select datetime,sum(main_sum_ya_gold),sum(main_sum_de_gold) ,sum(dice_sum_ya_gold),sum(dice_sum_de_gold)  from game_data_log where datetime like ? and type=? group by datetime desc";
            } else {
                hql = "select datetime,sum(sum_ya_gold),sum(sum_de_gold) from game_data_log where datetime like ? and type=? group by datetime desc";
            }
            if (type == -1) {
                hql = "select datetime,sum(sum_ya_gold),sum(sum_de_gold) from game_data_log where datetime like ? group by datetime desc ";
            }
        } else if (promoterId == 0) {
            if (type == 7) {
                hql = "select datetime,sum(main_sum_ya_gold),sum(main_sum_de_gold) ,sum(dice_sum_ya_gold),sum(dice_sum_de_gold) from game_data_log where datetime like ? and type=? and promoter_id=0  group by datetime desc";
            } else {
                hql = "select datetime,sum(sum_ya_gold),sum(sum_de_gold) from game_data_log where datetime like ? and type=? and promoter_id=0 group by datetime desc ";
            }
            if (type == -1) {
                hql = "select datetime,sum(sum_ya_gold),sum(sum_de_gold) from game_data_log where datetime like ? and promoter_id=0 group by datetime desc";
            }
        } else {
            List<Integer> promoterIds = new ArrayList();
            promoterIds.add(Integer.valueOf(promoterId));
            this.promoterServiceDao1.getPromotersByParentId(promoterIds, promoterId);
            proIds.append("(");
            for (Integer pid : promoterIds) {
                proIds.append(pid + ",");
            }
            proIds = new StringBuilder(proIds.substring(0, proIds.length() - 1));
            proIds.append(")");
            if (type == 7) {
                hql = "select datetime,sum(main_sum_ya_gold),sum(main_sum_de_gold) ,sum(dice_sum_ya_gold),sum(dice_sum_de_gold) from game_data_log where datetime like ? and type=? and promoter_id in " + proIds.toString() + " group by datetime desc ";
            } else {
                hql = "select datetime,sum(sum_ya_gold),sum(sum_de_gold) from game_data_log where datetime like ? and type=? and promoter_id in " + proIds.toString() + " group by datetime desc ";
            }
            if (type == -1) {
                hql = "select datetime,sum(sum_ya_gold),sum(sum_de_gold) from game_data_log where datetime like ? and promoter_id in " + proIds.toString() + " group by datetime desc";
            }
        }
        List<Object[]> logs = this.gameDataLogServiceDao.getListByDateAndType(hql, date, type);
        for (String d : dates) {
            Object[] log = (Object[]) null;
            for (Object[] o : logs) {
                if (o[0] != null) {
                    if (o[0].toString().equals(d)) {
                        log = o;
                        break;
                    }
                }
            }
            Map map = new HashMap();
            if (log != null) {
                map.put("date", log[0]);
                if (type == 7) {
                    double mainSumYaGold = Double.parseDouble(log[1].toString());
                    mainSumYaGold = MyUtil.doubleFormat(mainSumYaGold);

                    double mainSumDeGold = Double.parseDouble(log[2].toString());
                    mainSumDeGold = MyUtil.doubleFormat(mainSumDeGold);

                    double diceSumYaGold = Double.parseDouble(log[3].toString());
                    diceSumYaGold = MyUtil.doubleFormat(diceSumYaGold);

                    double diceSumDeGold = Double.parseDouble(log[4].toString());
                    diceSumDeGold = MyUtil.doubleFormat(diceSumDeGold);
                    map.put("mainSumYaGold", Double.valueOf(mainSumYaGold));
                    map.put("mainSumDeGold", Double.valueOf(mainSumDeGold));
                    map.put("mainResult", Double.valueOf(MyUtil.doubleFormat(mainSumYaGold - mainSumDeGold)));
                    map.put("diceSumYaGold", Double.valueOf(diceSumYaGold));
                    map.put("diceSumDeGold", Double.valueOf(diceSumDeGold));
                    map.put("diceResult", Double.valueOf(MyUtil.doubleFormat(diceSumYaGold - diceSumDeGold)));
                } else {
                    long sumYa = ((Number) log[1]).longValue();
                    long sumDe = ((Number) log[2]).longValue();
                    map.put("allPlay", Long.valueOf(sumYa));
                    map.put("allGet", Long.valueOf(sumDe));
                    map.put("allGain", Long.valueOf(sumYa - sumDe));
                    map.put("mainSumYaGold", Integer.valueOf(0));
                    map.put("mainSumDeGold", Integer.valueOf(0));
                    map.put("mainResult", Integer.valueOf(0));
                    map.put("diceSumYaGold", Integer.valueOf(0));
                    map.put("diceSumDeGold", Integer.valueOf(0));
                    map.put("diceResult", Integer.valueOf(0));
                    map.put("sumRobotYa", Integer.valueOf(0));
                    map.put("sumRobotDe", Integer.valueOf(0));
                }
                result.add(map);
            } else {
                map.put("date", d);
                if (type == 7) {
                    map.put("mainSumYaGold", Integer.valueOf(0));
                    map.put("mainSumDeGold", Integer.valueOf(0));
                    map.put("mainResult", Integer.valueOf(0));
                    map.put("diceSumYaGold", Integer.valueOf(0));
                    map.put("diceSumDeGold", Integer.valueOf(0));
                    map.put("diceResult", Integer.valueOf(0));
                } else if (type == 6) {
                    map.put("allPlay", Integer.valueOf(0));
                    map.put("allGet", Integer.valueOf(0));
                    map.put("allGain", Integer.valueOf(0));
                    map.put("sumRobotYa", Integer.valueOf(0));
                    map.put("sumRobotDe", Integer.valueOf(0));
                } else {
                    map.put("allPlay", Integer.valueOf(0));
                    map.put("allGet", Integer.valueOf(0));
                    map.put("allGain", Integer.valueOf(0));
                    map.put("mainSumYaGold", Integer.valueOf(0));
                    map.put("mainSumDeGold", Integer.valueOf(0));
                    map.put("mainResult", Integer.valueOf(0));
                    map.put("diceSumYaGold", Integer.valueOf(0));
                    map.put("diceSumDeGold", Integer.valueOf(0));
                    map.put("diceResult", Integer.valueOf(0));
                    map.put("sumRobotYa", Integer.valueOf(0));
                    map.put("sumRobotDe", Integer.valueOf(0));
                }
                result.add(map);
                System.out.println("map:" + map);
            }
        }
        return result.toArray();
    }

    private List<String> datesList(String date) {
        Calendar c = Calendar.getInstance();
        Calendar searchDate = Calendar.getInstance();
        searchDate.setTime(MyUtil.dateStrParse(date, "yyyy-MM"));
        int today = 0;
        if (MyUtil.CalendarIf(c, searchDate)) {
            today = c.get(5);
        } else {
            today = searchDate.getActualMaximum(5);
        }
        List dates = new ArrayList();
        int i = 1;
        while (i <= today) {
            searchDate.set(5, i);
            dates.add(MyUtil.dateFormatYMD_Sting(searchDate.getTime()));
            i++;
        }
        return dates;
    }

    public Map<String, Object> deleteRunStatistics(String date, IoSession session) {
        Map result = new HashMap();
        this.gameDataLogServiceDao.deleteByDate(date);
        result.put("success", Boolean.valueOf(true));
        result.put("message", "");
        return result;
    }

    public Object[] getPromotionList(IoSession session) {
        List result = new ArrayList();
        Map map = new HashMap();
        map.put("value", Integer.valueOf(-1));
        map.put("name", MyUtil.getProperties(Integer.valueOf(resultInt(session)), "sum"));
        result.add(map);
        map = new HashMap();
        map.put("value", Integer.valueOf(0));
        map.put("name", MyUtil.getProperties(Integer.valueOf(resultInt(session)), "zhiShuUser"));
        result.add(map);
        List<Promoter> promoters = this.promoterServiceDao1.q_CreateSqlList("from Promoter where parentId=0", new Object[0]);
        for (Promoter p : promoters) {
            map = new HashMap();
            map.put("value", Integer.valueOf(p.getId()));
            map.put("name", p.getUsername());
            result.add(map);
        }
        return result.toArray();
    }

}
